// index.js
// 获取应用实例
import myRequest from '../../utils/util'
const app = getApp()

Page({
  data: {
    swiper: [],
    list: []
  },
  page: 1,
  onSearch() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  onLoad() {
    this.fetchSwiper()
    this.fetchList(1)
  },
  async fetchSwiper() {
    const data = await myRequest({
      url: 'http://localhost:3000/swiper'
    })
    console.log(data)
    // wx.request({
    //   url: 'http://localhost:3000/swiper',
    //   success: (res) => {
    //     this.setData({
    //       swiper: res.data
    //     })
    //   }
    // })

  },
  fetchList(page) {
    wx.request({
      url: 'http://localhost:3000/list',
      method: 'GET',
      data: {
        _limit: 10,
        _page: page
      },
      success:(res) => {
        this.setData({
          list: page === 1 ? res.data :this.data.list.concat(res.data)
        })
      }
    })
  },
  onReachBottom() {
    this.page += 1;
    this.fetchList(this.page)
  },
  onPullDownRefresh() {
    this.page = 1;
    this.fetchList(1)
  },
  onClick(event) {
    const {id} = event.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/detail/detail?id='+id,
    })
  }
})
